Option Explicit On 
Option Strict On

Imports TreeCreeper01Interfaces

Friend Class cNeighbourhood
    Implements INeighbourhood


    Private pCenterPlant As IPlant
    Private pNeighbourhoodPlantsArrayList As ArrayList


    Sub New(ByVal CenterPlant As IPlant)
        pCenterPlant = CenterPlant
        pNeighbourhoodPlantsArrayList = New ArrayList
    End Sub


    Public ReadOnly Property CenterPlant() As IPlant Implements INeighbourhood.CenterPlant
        Get
            Return pCenterPlant
        End Get
    End Property


    Public ReadOnly Property CountOfNeighbours() As Integer Implements INeighbourhood.CountOfNeighbours
        Get
            Return pNeighbourhoodPlantsArrayList.Count()
        End Get
    End Property


    Friend Sub AddNeighbourPlant(ByVal NeighbourhoodPlant As INeighbourhoodPlant) Implements INeighbourhood.AddNeighbourhoodPlant
        pNeighbourhoodPlantsArrayList.Add(NeighbourhoodPlant)
    End Sub


    Public ReadOnly Property NeighbourhoodPlantByIndexBaseOne(ByVal Index As Integer) As TreeCreeper01Interfaces.INeighbourhoodPlant Implements TreeCreeper01Interfaces.INeighbourhood.NeighbourhoodPlantByIndexBaseOne
        Get
            Dim NeighbourPlant As INeighbourhoodPlant

            NeighbourPlant = CType(pNeighbourhoodPlantsArrayList.Item(Index - 1), INeighbourhoodPlant)

            Return NeighbourPlant
        End Get
    End Property


    'Friend ReadOnly Property NeighbourHoodPlantsArrayList() As ArrayList Implements INeighbourhood.NeighbourHoodPlantsArrayList
    '    Get
    '        Return pNeighbourhoodPlantsArrayList
    '    End Get
    'End Property




    Friend ReadOnly Property INeighbourhoodDiversityContrast() As INeighbourhoodDiversityContrast Implements INeighbourhood.NeighbourhoodDiversityContrast
        Get

            Dim NeighbourhoodPlantIndex As Integer
            Dim NeighbourhoodPlant As INeighbourhoodPlant

            Dim CumulativeTaxonimicDiversityForNeighbourhood As Double
            Dim ThisContrastValue As Double
            Dim MeanTaxonomicContastForNeighbourhood As Double

            Dim TheNeighbourhoodDiversityContrast As INeighbourhoodDiversityContrast

            CumulativeTaxonimicDiversityForNeighbourhood = 0
            For NeighbourhoodPlantIndex = 1 To CountOfNeighbours
                NeighbourhoodPlant = Me.NeighbourhoodPlantByIndexBaseOne(NeighbourhoodPlantIndex)
                ThisContrastValue = NeighbourhoodPlant.TaxonomicDistanceFromCenterPlant()
                CumulativeTaxonimicDiversityForNeighbourhood = CumulativeTaxonimicDiversityForNeighbourhood + ThisContrastValue
            Next

            If CountOfNeighbours = 0 Then
                ' Empry neighbourhoods should not participate on larger statistical calculations
                ' Return a flag value to notify caller about this
                MeanTaxonomicContastForNeighbourhood = Double.NaN
            Else
                MeanTaxonomicContastForNeighbourhood = CumulativeTaxonimicDiversityForNeighbourhood / CountOfNeighbours
            End If


            TheNeighbourhoodDiversityContrast = New cNeighbourhoodDiversityContrast(pCenterPlant, MeanTaxonomicContastForNeighbourhood)

            Return TheNeighbourhoodDiversityContrast

        End Get
    End Property



    Public Sub SortSpatially() Implements TreeCreeper01Interfaces.INeighbourhood.SortSpatially
        pNeighbourhoodPlantsArrayList.Sort()

    End Sub

End Class
